// 字节
// const a = [1, [2, [3, [4, null]]]](
// 反转成 b= [4,[3,[2,[1,null]]]]

(function main() {
  let a = [1, [2, [3, [4, null]]]];
  // 实现数组 b = [4,[3,[2,[1,null]]]]
  let path = [];
  while (a) {
    path.push(a.shift());
    a = a[0];
  }
  console.log(path, a);
  let real = [a];
  while (path.length) {
    let arr = [];
    arr.unshift(path.shift(), real[0] ? real.slice() : null);
    real = arr;
    console.log(real);
  }
  console.log(JSON.stringify(real));
})();

// 递归实现
// 这里的递归用的很巧妙，真的每次看递归的代码都有新的收获
(function main() {
  let a = [1, [2, [3, [4, null]]]];
  // 实现数组 b = [4,[3,[2,[1,null]]]]
  const reverse = (arr, tmp) => {
    if (arr[1] == null) return [arr[0], tmp];
    tmp = tmp ? [arr[0], tmp] : [arr[0], null];
    console.log(tmp);
    return reverse(arr[1], tmp);
  };

  console.log(JSON.stringify(reverse(a)));
})();

function fn(arr, tmp) {
  if (!arr[1]) return [arr[0], tmp];
  tmp = tmp ? [arr[0], tmp] : [arr[0], null];
  return fn(arr[1], tmp);
}
